﻿/****************************************************************************************************
	Copyright (C) 2010 RapidWebDev Organization (http://rapidwebdev.org)
	Author: Eunge, Legal Name: Jian Liu, Email: eunge.liu@RapidWebDev.org

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ****************************************************************************************************/

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;

namespace BaoJianSoft.Common.Data
{
	/// <summary>
	/// DataContext configuration which defines all data context used in platform.
	/// </summary>
	public class DataContextConfiguration : ConfigurationSection
	{
		[ConfigurationProperty("settings")]
		public DataContextSettings DataContextSettings
		{
			get { return this["settings"] as DataContextSettings; }
		}
	}

	/// <summary>
	/// DataContextSetting configuration collection.
	/// </summary>
	public class DataContextSettings : ConfigurationElementCollection
	{
		/// <summary>
		/// When overridden in a derived class, creates a new System.Configuration.ConfigurationElement.
		/// </summary>
		/// <returns></returns>
		protected override ConfigurationElement CreateNewElement()
		{
			return new DataContextSetting();
		}

		/// <summary>
		/// Gets the element key for a specified configuration element when overridden in a derived class.
		/// </summary>
		/// <param name="element"></param>
		/// <returns></returns>
		protected override object GetElementKey(ConfigurationElement element)
		{
			DataContextSetting dataContextSetting = element as DataContextSetting;
			return dataContextSetting.Name;
		}

		/// <summary>
		/// Gets the type of the System.Configuration.ConfigurationElementCollection.
		/// </summary>
		public override ConfigurationElementCollectionType CollectionType
		{
			get { return ConfigurationElementCollectionType.BasicMap; }
		}

		/// <summary>
		/// Gets the name used to identify this collection of elements in the configuration file when overridden in a derived class.
		/// </summary>
		protected override string ElementName
		{
			get { return "setting"; }
		}
	}

	/// <summary>
	/// DataContext setting class.
	/// </summary>
	public class DataContextSetting : ConfigurationElement
	{
		/// <summary>
		/// Sets/gets data context name.
		/// </summary>
		[ConfigurationProperty("name", IsRequired = true, IsKey = true)]
		public string Name
		{
			get { return this["name"] as string; }
			set { this["name"] = value; }
		}

		/// <summary>
		/// Sets/gets data context type.
		/// </summary>
		[ConfigurationProperty("type", IsRequired = true)]
		public string Type
		{
			get { return this["type"] as string; }
			set { this["type"] = value; }
		}

		/// <summary>
		/// Sets/gets database connection string name.
		/// </summary>
		[ConfigurationProperty("connectionString", IsRequired = true)]
		public string ConnectionString
		{
			get { return this["connectionString"] as string; }
			set { this["connectionString"] = value; }
		}
	}
}

